/*
 * @Author: moeWang
 * @Date: 2021-09-08 11:20:04
 * @LastEditTime: 2021-09-08 13:23:26
 * @LastEditors: moeWang
 * @Description: 命令模式
 * @FilePath: \front-end-design-mode\code\Command.js
 */

/* 
    本质是对命令封装，拆分发出命令的责任和执行命令的责任
    优点：降低对象耦合度，易扩展组合命令，调用同一方法实现不同功能
*/

// 命令
var CreateCommand = function (receiver) {
    this.receiver = receiver
}
CreateCommand.prototype.execute = function () {
    this.receiver.action()
}

// 接收者
var TVOn = function () { }
TVOn.prototype.action = function () {
    console.log('TV on now');
}

var TVOff = function () { }
TVOff.prototype.action = function () {
    console.log('TV off now');
}

// 调用者
var Invoker = function (tvOnCommand, tvOffCommand) {
    this.tvOnCommand = tvOnCommand
    this.tvOffCommand = tvOffCommand
}
Invoker.prototype.tvOn = function () {
    this.tvOnCommand.execute()
}
Invoker.prototype.tvOff = function () {
    this.tvOffCommand.execute()
}

var tvOnCommand = new CreateCommand(new TVOn())
var tvOffCommand = new CreateCommand(new TVOff())

var myInvoker = new Invoker(tvOnCommand, tvOffCommand)
myInvoker.tvOn()
myInvoker.tvOff()